/**
 * @Copyright ®2015 Sinosoft Co. Ltd. All rights reserved.
 * 项目名称 : 网站服务平台
 * 创建日期 : 2017-08-11
 * 修改历史 :
 * 1. [2017-08-11]创建文件 by chenxin
 */
package com.gavinwind.inspay.paycenter.core.service.impl;

import com.gavinwind.inspay.api.paychannel.apiservice.service.GatewayPayApiService;
import com.gavinwind.inspay.api.paychannel.apiservice.service.PayInTimeApiService;
import com.gavinwind.inspay.base.BaseService;
import com.gavinwind.inspay.common.config.ENUM_PAY_STATUS;
import com.gavinwind.inspay.common.config.ENUM_PAY_WAYS;
import com.gavinwind.inspay.common.config.SystemConstants;
import com.gavinwind.inspay.common.exception.pay.PayBusinessException;
import com.gavinwind.inspay.common.model.PayRequestDTO;
import com.gavinwind.inspay.common.model.PayResponseDTO;
import com.gavinwind.inspay.common.util.DateUtil;
import com.gavinwind.inspay.paycenter.core.generated.model.PayTradeMain;
import com.gavinwind.inspay.paycenter.core.generated.model.PayTradeOrder;
import com.gavinwind.inspay.paycenter.core.service.PayCenterCoreService;
import com.gavinwind.inspay.paycenter.router.service.PayChannelRouterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

/**
 * 【支付中心核心服务接口实现类】
 * @author chenxin
 * @date 2017-08-11 下午5:21 
 * @version V1.0
 */
@Service
public class PayCenterCoreServiceImpl extends BaseService implements PayCenterCoreService{
    @Autowired
    private PayChannelRouterService payChannelRouterService;
    /**
     * 获得支付网关URL
     * <p>获取网关URL,不持久化数据,返回的URL与业务号绑定</p>
     * @param cRequestDTO
     * @return
     */
    public String getPayGatewayUrl(PayRequestDTO cRequestDTO) {
        //1.获取请求参数,解析为变量
        String tPayChnlCode = cRequestDTO.getThirdpayCode();
        //2.调用支付规则引擎,校验支付规则
        checkPayRulesEngine(cRequestDTO);
        //2.1 规则校验通过
        //2.2 规则校验失败
        //3.根据支付通道CODE通过支付路由获取相应支付渠道处理Dealer
        String tPayThirdChnnlCode = payChannelRouterService.routerPayChnl(cRequestDTO);
        GatewayPayApiService tGatewayPayApiService = payChannelRouterService.generateGatewayPayApiService(tPayThirdChnnlCode);
        //4.根据请求参数信息封装网关地址请求DTO
        //5.调用支付通道处理Dealer,获取网关URL
        String tPayGatewayUrl = tGatewayPayApiService.getPayGatewayUrl(cRequestDTO);
        //6.返回URL
        return tPayGatewayUrl;
    }

    /**
     * 处理实时支付请求
     * @param cRequestDTO
     * @return
     */
    public PayResponseDTO dealPayInTime(PayRequestDTO cRequestDTO) throws PayBusinessException {
        //0.校验支付请求是否重复
        //1.获取请求参数,解析为变量
        String tPayChnlCode = cRequestDTO.getThirdpayCode();
        //2.调用支付规则引擎,校验支付规则
        checkPayRulesEngine(cRequestDTO);
        //2.1 规则校验通过 TODO
        //2.2 规则校验失败 TODO
        //3.保存支付请求订单数据
        dealPaymentOrder(cRequestDTO);
        //4.根据支付通道CODE通过支付路由获取相应支付渠道处理Dealer
        String tPayThirdChnnlCode = payChannelRouterService.routerPayChnl(cRequestDTO);
        PayInTimeApiService tPayInTimeApiService = payChannelRouterService.generatePayIntimeApiService(tPayChnlCode);
        PayResponseDTO payResponseDTO = tPayInTimeApiService.payInTime(cRequestDTO);
        //返回结果如果需要差异化处理,可以后续优化
        return payResponseDTO;
    }

    /**
     * 处理支付订单信息
     * @param cRequestDTO 支付请求信息
     */
    private void dealPaymentOrder(PayRequestDTO cRequestDTO) {
        Date currentDate = DateUtil.getCurrentDate();
        String bizType = cRequestDTO.getBizType();
        String bizNo = cRequestDTO.getBizNo();
        String accoutName = cRequestDTO.getAccountName();
        String accountNo = cRequestDTO.getPayAccountNo();
        String bankCode = cRequestDTO.getBankCode();
        String accountIdNo = cRequestDTO.getAccountIdNo();
        String accountIdType = cRequestDTO.getAccountIdType();
        String merchantCode = cRequestDTO.getSourceCode();
        String mobile = cRequestDTO.getAccountMobile();
        String openBank = cRequestDTO.getOpenBank();
        String payType = cRequestDTO.getPayType();
        String serialNo = cRequestDTO.getSerialNo();
        Integer payAmount = cRequestDTO.getPayMoney();
        String thirdPayCode = cRequestDTO.getThirdpayCode();
        String payWay = cRequestDTO.getPayWays();
        PayTradeOrder tradeOrder = new PayTradeOrder();
        tradeOrder.setBizType(bizType);
        tradeOrder.setBizNo(bizNo);
        tradeOrder.setAccountName(accoutName);
        tradeOrder.setAccountNo(accountNo);
        tradeOrder.setBankCode(bankCode);
        tradeOrder.setCertType("K");//C-信用卡  K-借记卡  Z-存折
        tradeOrder.setCreateDate(currentDate);
        tradeOrder.setCreatorId(SystemConstants.SYS_OPERATOR);
        tradeOrder.setCurrency("CNY");
        tradeOrder.setIdNo(accountIdNo);
        tradeOrder.setIdType(accountIdType);
        tradeOrder.setMerchantCode(merchantCode);
        tradeOrder.setMobile(mobile);
        tradeOrder.setModifierId(SystemConstants.SYS_OPERATOR);
        tradeOrder.setModifyDate(currentDate);
        tradeOrder.setOpenBank(openBank);
//        tradeOrder.setOpenBankArea();
        tradeOrder.setPayType(payType);
        tradeOrder.setPayStatus(ENUM_PAY_STATUS.PAYING.code());
        //生成payno
        String payNo = "";
        tradeOrder.setPayNo(payNo);
        cRequestDTO.setPayNo(payNo);
        tradeOrder.setSerialNo(serialNo);
        tradeOrder.setTradeAmount(payAmount);
//        tradeOrder.setTradeDesc();
        tradeOrder.setThirdpayCode(thirdPayCode);
        tradeOrder.setPayFlag("S"); //S-收费   F-付费
        PayTradeMain tradeMain = new PayTradeMain();
        tradeMain.setPayNo(payNo);
        tradeMain.setThirdpayCode(thirdPayCode);
//        tradeMain.setTradeDesc();
        tradeMain.setSerialNo(serialNo);
        tradeMain.setBizNo(bizNo);
        tradeMain.setBizType(bizType);
        tradeMain.setCheckDate();
        tradeMain.setCheckFlag();
        tradeMain.setCreateDate(currentDate);
        tradeMain.setCreatorId(SystemConstants.SYS_OPERATOR);
        tradeMain.setCurrency("CNY");
        tradeMain.setMerchantCode(merchantCode);
//        tradeMain.setThirdPayNo();
        tradeMain.setTradeTimes(0);
        /***********   查询封装交易账户 start   ***************/
        tradeMain.setReceiveAccountMobile();
        tradeMain.setReceiveOpenBankArea();
        tradeMain.setReceiveAccountName();
        tradeMain.setReceiveOpenBank();
        tradeMain.setReceiveAccountNo();
        tradeMain.setReceiveBankName();
        tradeMain.setReceiveBankCode();
        tradeMain.setReceiveCertType();
        tradeMain.setReceiveIdNo();
        tradeMain.setReceiveIdType();
        /***********   查询封装交易账户 end   ***************/
        tradeMain.setPayAccountMobile(mobile);
        tradeMain.setPayAmount(payAmount);
        tradeMain.setPayAccountName(accoutName);
        tradeMain.setPayBankCode(bankCode);
        //通过bankcode获取bankname
        tradeMain.setPayBankName();
        tradeMain.setPayCertType("K");
        tradeMain.setPayAccountNo(accountNo);
        tradeMain.setPayIdNo(accountIdNo);
        tradeMain.setPayIdType(accountIdType);
        tradeMain.setPayOpenBank(openBank);
//        tradeMain.setPayOpenBankArea();
        tradeMain.setPayWay(payWay);
//        tradeMain.setPayDate();
        tradeMain.setPayType(payType);
        tradeMain.setPayStatus(ENUM_PAY_STATUS.PAYING.code());

    }

    private void checkPayRulesEngine(PayRequestDTO cRequestDTO) {
        //TODO
    }


}
